package day_2022_9_to_12.Day2;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author haomin
 * @date 2022/11/28 22:29
 **/
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
}

public class Day1_Review {
    class Solution {
        public int kthLargest(TreeNode root, int k) {
            if(root == null || k <= 0) return -1;
            Deque<TreeNode> s = new LinkedList<>();
            while(!s.isEmpty() || root != null) {
                while(root != null) {
                    s.push(root);
                    root = root.right;
                }
                root = s.pop();
                if(--k == 0) return root.val;
                root = root.left;
            }
            return -1;
        }
    }
}